<html>
	<head>
		<title>nzbed for Audiobooks</title>
		<script type="text/javascript">

		var httpRequest;
		var httpRequestCallbackFunction;
		var debug = true;
		
		var gbooksApiUrl = 'http://books.google.com/books/feeds/volumes';

		function Nzbed(queryString)
		{
			var author = queryString.split(" - ")[0];
			var title = queryString.split(" - ")[1];
			_httpGet( gbooksApiUrl + "?q=" + URLEncode(queryString) , _httpResponse);
		}

		
		
		// ====================================================================
		//       URLEncode and URLDecode functions
		//
		// Copyright Albion Research Ltd. 2002
		// http://www.albionresearch.com/
		// ====================================================================

		function URLEncode( string )
		{
			// The Javascript escape and unescape functions do not correspond
			// with what browsers actually do...
			var SAFECHARS = "0123456789" +					// Numeric
							"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
							"abcdefghijklmnopqrstuvwxyz" +
							"-_.!~*'()";					// RFC2396 Mark characters
			var HEX = "0123456789ABCDEF";

			var plaintext = string;
			var encoded = "";
			for (var i = 0; i < plaintext.length; i++ ) {
				var ch = plaintext.charAt(i);
				if (ch == " ") {
					encoded += "+";				// x-www-urlencoded, rather than %20
				} else if (SAFECHARS.indexOf(ch) != -1) {
					encoded += ch;
				} else {
					var charCode = ch.charCodeAt(0);
					if (charCode > 255) {
						alert( "Unicode Character '" 
								+ ch 
								+ "' cannot be encoded using standard URL encoding.\n" +
								  "(URL encoding only supports 8-bit characters.)\n" +
								  "A space (+) will be substituted." );
						encoded += "+";
					} else {
						encoded += "%";
						encoded += HEX.charAt((charCode >> 4) & 0xF);
						encoded += HEX.charAt(charCode & 0xF);
					}
				}
			} // for

			return encoded;
		};

		function HtmlDecode(s)
		{	
			var out = "";
			if (s==null) return;

			var l = s.length;	
			for (var i=0; i<l; i++)
			{
				var ch = s.charAt(i);
				   

				if (ch == '&')
				{
					var semicolonIndex = s.indexOf(';', i+1);
						
					if (semicolonIndex > 0)
					{
						var entity = s.substring(i + 1, semicolonIndex);

									if (entity.length > 1 && entity.charAt(0) == '#')
									{
							if (entity.charAt(1) == 'x' || entity.charAt(1) == 'X')
								ch = String.fromCharCode(eval('0'+entity.substring(1)));
							else
								ch = String.fromCharCode(eval(entity.substring(1)));
						}
						i = semicolonIndex;
					}
				}     
				out += ch;
			}

			return out;
		}
		
		
		/*
		 * HTTP Request
		 */

		function _httpResponse()
		{
			if ( debug ) alert( httpRequest.responseText );
			//_formFill( httpRequest.responseXML );
		}

		function _httpExecuteCallback()
		{
			if ( debug ) alert( 'readyState = ' + httpRequest.readyState + '\nstatus = ' + httpRequest.status );
			if (httpRequestCallbackFunction != null) {
				if (httpRequest.readyState == 4) {
					if (httpRequest.status == 200 || httpRequest.status == 0) {
						httpRequestCallbackFunction();
						httpRequestCallbackFunction = null;
					}
				}
			}
		}

		function _httpGet(url, callbackFunction)
		{
			if (debug) alert( 'Get Url: ' + url );
			httpRequest = false;
			httpRequestCallbackFunction = callbackFunction;
			httpRequest = new XMLHttpRequest();
			httpRequest.onreadystatechange = _httpExecuteCallback;
			httpRequest.open('GET', url, true);
			httpRequest.send(null);
		}
		
		</script>
	</head>
	<body>
		<div id="pageContent">
			<form name="SearchForm" id="frmSearch">
				<input type="text" name="queryString" id="inpQueryString" />
				<input type="button" name="SearchButton" value="Search" id="btnSearch" onclick="Nzbed(document.getElementById('inpQueryString').value)" />
			</form>
		</div>
	</body>